#include <iostream>
#include <vector>
#include <climits>

using namespace std;

// https://leetcode.cn/problems/maximum-subarray/

class Solution
{
public:
    int maxSubArray(vector<int> &nums)
    {
        int ans = INT_MIN;
        int pre = 0;
        int curSum = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (pre >= 0)
            {
                curSum = pre + nums[i];
                ans = max(curSum, ans);
                pre = curSum;
            }
            else
            {
                ans = max(ans, nums[i]);
                pre = nums[i];
            }
        }
        return ans;
    }
};